/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sdc.event;

import java.util.Map;
import java.util.Map.Entry;
import sdc.Address;
import sdc.utility.MersenneTwisterRNG;
import sdc.component.type.ComponentType;
import sdc.net.server.appli.ApplicationServer;
import sdc.net.server.comp.ComponentServer;
import sdc.net.server.comp.ComponentServerManager;

/**
 *
 * @author yokoe
 */
public class ArrivalEvent extends Event{

	ApplicationServer applicationServer;
        private double onceServiceTime;

	public ArrivalEvent(double eventTime, ApplicationServer applicationServer) {
		super(eventTime);
		this.applicationServer = applicationServer;
	}

	@Override
	public void fire() {
		Map<ComponentType, Address> addresses = applicationServer.getUseComponentMap();

		for(Entry<ComponentType, Address> entry : addresses.entrySet())
		{
			ComponentServer componentServer = ComponentServerManager.INSTANCE.get(entry.getValue());

			componentServer.arrival(entry.getKey(), this.getEventTime());
		}

                double mu = applicationServer.getMu();
		double nextEventTime = this.getEventTime() + MersenneTwisterRNG.INSTANCE.nextExponential(mu);
		ArrivalEvent arrivalEvent = new ArrivalEvent(nextEventTime, applicationServer);
                onceServiceTime = nextEventTime - this.getEventTime();
		EventQueue.INSTANCE.addEvent(arrivalEvent);
                //System.out.println("Once Service Time:" + onceServiceTime);
	}

        public double getOnceServiceTime(){
            return onceServiceTime;
        }
}
